//
//  HomeVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/13.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Moya

class HomeVC: BaseVC {
  
    
    fileprivate var oldNetworkState: Bool = true

    fileprivate var placeholderView: NoDataV?
    fileprivate lazy var allOrderVC: OrderStateVC = {
        let vc = OrderStateVC()
        vc.parentVC = self
        vc.type = .home
        self.view.addSubview(vc.view)
        self.addChildViewController(vc)
        vc.view.snp.makeConstraints { (make) in
            make.top.height.left.width.equalToSuperview()
        }
        return vc
    }()
    
    fileprivate lazy var refuseView: RefuseAccessNetworkView = {
        let view = RefuseAccessNetworkView()
        weak var weakSelf = self
        view.refreshClosure = {
            if SJApp.isAlowNetWork {
                weakSelf?.allOrderVC.updateData()
                view.isHidden = true
            }
        }
        return view
    }()


    fileprivate lazy var notConnectedV: NoNetworkView = {
        let view = NoNetworkView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 44))
        return view
    }()
  
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let t = DispatchTime.now() + 0.1
        DispatchQueue.main.asyncAfter(deadline: t) {
            // 如果没有登录,跳转登录界面
            if !Account.isLogin {
                let vc =  LoginVC()
                weak var weakSelf = self
                vc.refreshBeforeClosure = {
                    if SJApp.isAlowNetWork {
                        weakSelf?.refuseView.isHidden = true
                        weakSelf?.allOrderVC.updateData()
                    }
                }
                let longinVC = SJBaseNavigationC(rootViewController:vc)
                self.present(longinVC, animated: false, completion: nil)
            }
        }
        
        
        create()

        // app第一次进入,判断是否开启网络权限
        SJApp.onFirstStart {
            if !SJApp.isAlowNetWork {
                refuseView.isHidden = false
                self.navigationController?.view.addSubview(refuseView)
            }
        }
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(reloadData(noti:)), name: Notification.Name.updateHome.updateHomeOrderData, object: nil)
    }
  
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        blueNavigationBackgroud()
        GlobalConfig.baiduStatisticsPageViewStart("工作")
    }
  
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        GlobalConfig.baiduStatisticsPageViewEnd("工作")
    }
  
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    deinit {
        GlobalConfig.notificationCenter.removeObserver(self)
    }
    func reloadData(noti:NSNotification) {
        self.allOrderVC.updateData()
    }
}

// MARK: - action

extension HomeVC {
    // 退出登录后更新界面到默认
    @objc fileprivate func logoutAction() {
        placeholderView?.removeFromSuperview()
    }
}
// MARK: - fileprivate

extension HomeVC {
  fileprivate func create() {
    view.backgroundColor = GlobalConfig.BGWhiteColor
    
    let leftItem = UIBarButtonItem(image: UIImage(named: "home_right_item")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(orderCenterNext))
    navigationItem.rightBarButtonItem = leftItem
    
    
    let profileitem = UIBarButtonItem(image: UIImage(named: "nav-menu-icon")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(profileNext))
    navigationItem.leftBarButtonItem = profileitem
    
    if Account.isLogin {
        self.allOrderVC.updateData()
    }
    self.title = "工作"
  }
  
    @objc fileprivate func noNetworkAction() {
        if let _ = refuseView.superview {
            refuseView.removeFromSuperview()
        }
    }
  
    @objc fileprivate func profileNext() {
        let vc = ProfileVC()
        navigationController?.pushViewController(vc, animated: true)
    }
    @objc fileprivate func orderCenterNext() {
        let vc = OrderCenterVC()
        navigationController?.pushViewController(vc, animated: true)
    }
}








